{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Downloading Dependencies and Configuring your Environment\n",
    "\n",
    "In this step we will download tools, clone sources from GitHub, and configure your environment.Some of the steps will require downloading packages to your host computer while some will be downloaded to your PYNQ board. These will be used in the remainder of this Tutorial. \n",
    "\n",
    "As stated in the introduction, this guide demonstrates installation on a Linux host computer, but these instructions can also be followed on a Windows host computer using Cygwin. Where the Windows instructions differ, this tutorial will make every effort to highlight the difference. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Host Dependencies\n",
    "\n",
    "You will need to install the following programs on your host computer:\n",
    "\n",
    "   1. Git [(Windows & Linux Tutorial)](https://www.atlassian.com/git/tutorials/install-git)\n",
    "   2. Xilinx Vivado WebPack 2017.4 [(Windows & Linux Tutorial - See Chapter 3)](https://www.xilinx.com/support/documentation/sw_manuals/xilinx2017_1/ug973-vivado-release-notes-install-license.pdf)\n",
    "   3. Cygwin (Windows only, [Tutorial](http://www.mcclean-cooper.com/valentino/cygwin_install/))\n",
    "\n",
    "## Repositories\n",
    "   \n",
    "Once these tools are installed you need to clone the following repositories on your host computer. These will clone the RISC-V-On-PYNQ, picorv32, and riscv-gnu-toolchain repositories. We have cloned these directories to `/home/xilinx` on our host machine. \n",
    "\n",
    "On Linux, run these commands from a terminal. On Windows you can use Cygwin (with git installed) or use the Git Bash shell.\n",
    "\n",
    "This command clones the git repository containing these notebooks, source files, and the PicoRV32 Processor to your host machine:\n",
    "\n",
    "    git clone --recursive https://github.com/drichmond/RISC-V-On-PYNQ ~/PYNQ-RISC-V\n",
    "    \n",
    "**Optional:** If you wish, you can also clone the RISC-V cross compiler source to your host machine. This will take substantially less time to build than on the PYNQ board, but will require moving programs from your host to the PYNQ board. \n",
    "\n",
    "    git clone --recursive https://github.com/riscv/riscv-gnu-toolchain ~/riscv-gnu-toolchain"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PYNQ Dependencies\n",
    "\n",
    "First, check that your PYNQ board has an internet connection. Run the following cell to test the internet connection on your PYNQ board. You should see 10 responses from xilinx.com. If you do not see 10 responses then check your PYNQ Board's internet connection and try again. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!ping xilinx.com -c 10 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PYNQ Image V2.0\n",
    "\n",
    "If you are using the PYNQ Image v2.0 SD Card you must update the repository list. \n",
    "\n",
    "Run the following cell to update your APT sources list. It will check if you are running PYNQ Image v2.0 and update your repository list if necessary. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "REVISION = !cat /home/xilinx/REVISION\n",
    "if('Release 2017_08_17 8123713' == REVISION[0]):\n",
    "\n",
    "    !sed -i 's/ubuntu-ports/ubuntu/' /etc/apt/sources.list.d/multistrap-wily.list\n",
    "    !sed -i 's/ports/old-releases/' /etc/apt/sources.list.d/multistrap-wily.list\n",
    "\n",
    "    !apt update\n",
    "else:\n",
    "    print(\"PYNQ is Up-To-Date!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Downloading Dependencies\n",
    "\n",
    "Now that we have verified that the PYNQ board is connected to the internet, and we have updated the APT sources we can download and install dependencies.  \n",
    "\n",
    "Run the following cell to install the dependencies using apt:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!apt -y install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev git"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PYNQ Board Repositories\n",
    "\n",
    "You will need two source repositories on your PYNQ board. \n",
    "\n",
    "### RISC-V Gnu Toolchain\n",
    "\n",
    "First, we need the sources from RISC-V GNU Toolchain. We will build these tools in [Compiling the RISC-V GCC Toolchain and Building it on your PYNQ Board](3-Compiling-RISC-V-GCC-Toolchain).\n",
    "\n",
    "The this cell downloads the tools from the [RISC-V Tools Project](https://github.com/riscv/riscv-tools). This cell depends heavily on the speed of your internet connection and can take hours. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone --recursive https://github.com/riscv/riscv-gnu-toolchain /home/xilinx/riscv-gnu-toolchain"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The clone of riscv-qemu might fail. This is fine. Verify that the rest of the repository was cloned successfully by running the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls -lh /home/xilinx/riscv-gnu-toolchain"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### RISC-V On PYNQ\n",
    "\n",
    "Second, you will want a clone of the RISC-V-On-PYNQ repository on your PYNQ board. \n",
    "\n",
    "Run the following cell to clone this repository:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone --recursive https://github.com/drichmond/RISC-V-On-PYNQ /home/xilinx/RISC-V-On-PYNQ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "That's it! You are now ready to move on to the next steps:\n",
    "\n",
    "- [Creating a FPGA Bitstream](2-Creating-A-Bitstream.ipynb)\n",
    "\n",
    "- [Compiling the RISC-V GCC Toolchain](3-Compiling-RISC-V-GCC-Toolchain.ipynb)\n",
    "\n",
    "These steps can be completed in parallel and must be finished before progressing."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
